<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<jsp:include page="../include.jsp" flush="true"/>
	<script type="text/javascript">
	var grid;
	var win;
	var url;
	$(function(){
		var user_id = <%=session.getAttribute("user_id")%>
		var user_role = <%=session.getAttribute("user_role_id")%>
		if(user_role != 1 && user_role != 5){
			$('#query-form').hide();
			//$('#subLayout').layout('remove','north');
		}
		if (user_role != 3 && user_role != 6) {
			$("#isGroupReport").hide();
		}
	})
	$(function() {
		//grid = $('#user_info').datagrid();
		$('#btn-save,#btn-cancel').linkbutton();
		win = $('#user-window').window( {
			collapsible : false,//折叠
			minimizable : false,//最小化
			maximizable : false,//最大化
			draggable : true,//拖动
			resizable : false,//改变大小
			modal : true,
			closed : true
		});
	});
/////////////////////////////////////////////////////////////////////////
	function loadbody(stu_id){
		using('datagrid', function(){
			$('#table_body').datagrid({
				collapsible: true,
				fitColumns:true,
				fit:true,
				width:'auto',
				height:'auto',
				method:'post',
				toolbar:toolbarbody,
				nowrap: false,
				fitColumns:'true',
				striped: true,
				url:'b01!queryReportBody?stu_id='+stu_id,
				showFooter: true,
				idField:'data_id',
				sortOrder: 'desc',//列排序
				columns:[[
					{field:'ck',checkbox:true,width:2},
					{field:'data_id',title:'资料ID',align:'center',width:80,hidden:true},
					{field:'data_path',title:'路径',align:'center',width:80,hidden:true},
					{field:'data_name',title:'标题',align:'center',width:120},
					{field:'data_note',title:'备注',align:'center',editor:'text',width:120},
					{field:'data_type_id',title:'数据类型',align:'center',editor:'text',width:10,hidden:true},
					{field:'data_cretime',title:'上传时间',align:'center',editor:'text',width:40},
					{field:'data_path',title:'路径',align:'center',editor:'text',width:70,hidden:true},
					{field:'data_owner_id',title:'所属教师',align:'center',editor:'text',width:120,hidden:true}
				]],
				rownumbers:true,
				singleSelect:true
			});
		});
	}
/////////////////////////////////////////////////////////////////////////
	function append() {
		$("font").remove();
		reject();
		win.window('open');
		status = 'enable';
		$('#userform').form('clear');
		url ='b01!uploadReport?user_id='+<%=session.getAttribute("user_id")%>;
	}
	
	function appendbody() {
		$("font").remove();
		rejectbody();
		$("#isGroupReport").hide();
		win.window('open');
		status = 'enable';
		$('#userform').form('clear');
		url ='b01!uploadSupport?user_id='+<%=session.getAttribute("user_id")%>;
	}
	
	function remove() {
		var rows = $('#user_info').datagrid('getSelected');
		if(!rows || rows.data_id == null){
			show('删除的Report不存在!');
			return false;
		}
		$.messager.confirm('提示','确定删除Report：'+rows.data_name+' 吗?',function(result){  
            if (result){  
                $.post('b01!removeReport?data_id='+rows.data_id,function(message){  
                    $('#user_info').datagrid('reload');
                    show(message.returnMsg);
                });
            } 
            reject();
        });
	}
	
	function removebody() {
		var rows = $('#table_body').datagrid('getSelected');
		if(!rows){
			show('请先选择一行数据');
			return false;
		}
		$.messager.confirm('提示','确定删除文件：'+rows.data_name+' 吗?',function(result){  
            if (result){  
                $.post('b01!removeReport?data_id='+rows.data_id,function(message){  
                    $('#table_body').datagrid('reload');
                    show(message.returnMsg);
                });
            } 
            rejectbody();
        });
	}
	
	function save() {
		var uploadpath = $("#upload").val();
		$("font").remove();
		if(uploadpath == null ||uploadpath == ""){
			$("#upload").focus();
			$("#upload").after("<font color='red'>不能为空</font>");
			return false;
		}
		$('#userform').form('submit', {
			url : url,
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(message) {
				win.window('close');
				$('#user_info').datagrid('reload');
				$('#table_body').datagrid('reload');
				show(message.returnMsg);
			}
		});
	}
	
	function query(){
		var params = $('#user_info').datagrid('options').queryParams; //先取得 datagrid 的查询参数  
        var fields =$('#query-form').serializeArray(); //自动序列化表单元素为JSON对象  
        $.each( fields, function(i, field){
            params[field.name] = field.value; //设置查询参数  
        });   
        $('#user_info').datagrid('reload'); //设置好查询参数 reload 一下就可以了
	}
	function reject(){
		$('#user_info').datagrid('clearSelections');
	} 
	function rejectbody(){
		$('#table_body').datagrid('clearSelections');
	}
	function download(){
		var rows = $('#user_info').datagrid('getSelected');
		if(!rows){
			show("请先选择一条数据");
		}
		if (rows.data_id == null) {
			show(rows.USER_NAME+"暂未提交报告");
			return false;
		}
		document.location.href = "<%=basePath%>"+rows.data_path;
	}
	function downloadbody(){
		var rows = $('#table_body').datagrid('getSelected');
		if(!rows){
			show("请先选择一行数据");
		}
		document.location.href = "<%=basePath%>"+rows.data_path;
	}
	function closeWindow() {
		win.window('close');
		rejectbody();
	}
</script>
	<script type="text/javascript">
		$(function(){
			var lastIndex;
			$('#user_info').datagrid({
				width:'auto',
				fit:true,
				height:'auto',
				method:'post',
				nowrap: false,
				fitColumns:'true',
				toolbar:toolbar,
				striped: true,
				url:'b01!queryReportHead?user_role_id='+<%=session.getAttribute("user_role_id")%>+'&user_id='+<%=session.getAttribute("user_id")%>,
				showFooter: true,
				sortName: 'STU_ID',
				idField:'STU_ID',
				sortOrder:'desc',//列排序
				columns:[[
					{field:'ck',checkbox:true,width:2}, //显示复选框
					{field:'STU_ID',title:'学号',align:'center',width:80},
					{field:'USER_NAME',title:'姓名',align:'center',width:50},
					{field:'STU_GROUP_ID',title:'所在组',align:'center',editor:'text',width:70},
					{field:'STU_MISSION',title:'个人任务',align:'center',editor:'text',width:150},
					{field:'data_id',title:'报告ID',align:'center',editor:'text',width:70,hidden:true},
					{field:'data_name',title:'报告标题',align:'center',editor:'text',width:120},
					{field:'data_cretime',title:'提交时间',align:'center',editor:'text',width:100},
					{field:'data_owner_id',title:'报告拥有者',align:'center',editor:'text',width:20,hidden:true},
					{field:'data_note',title:'备注',align:'center',editor:'text',width:120},
					{field:'data_report_flag',title:'小组报告',align:'center',editor:'text',width:50,
						formatter: function(value,row,index){
							if (value == null){
								return "暂未提交!";
							} else if(value == "Y"){
								return "是";
							}else {
								return "否";
							}
						}
					},
					{field:'data_path',title:'文件路径',align:'center',editor:'text',width:120,hidden:true}
				]],
				rownumbers:true,
				singleSelect:true,
				onDblClickRow:function(){
					var rows = $('#user_info').datagrid('getSelected');
					loadbody(rows.STU_ID);
					loadbody(rows.STU_ID);
				},
				onLoadSuccess:function(){
               		$('#user_info').datagrid('clearSelections');
            	}
			});
		});
		
	</script>
  </head>
  <body>
	<div id="subLayout" class="easyui-layout" data-options="fit:true">
<!-- north -->
		<div data-options="region:'north',border:true,split:false" style="height:40px;">
			<form id="query-form" method="post" style="padding-left:10px;">
		  		学号<input type="text" class="easyui-validatebox" name="stuId" style="width:90px;">&nbsp;
		  		姓名<input type="text" class="easyui-validatebox" name="userName" style="width:90px;"></input>&nbsp;
				小组报告<select name="dataReportFlag">
							<option value=""></option>
							<option value="Y">是</option>
							<option value="N">否</option>
						</select>&nbsp;
				文件类型<input name="typeId" class="easyui-combogrid" style="width:90px" data-options="
					panelWidth: 200,
					idField: 'type_id',
					textField: 'type_id',
					url: 'b01!queryDataType',
					columns: [[
						{field:'type_name',title:'文件属性',width:80},
						{field:'type_id',title:'文件类型',width:120}
					]],
					fitColumns: true
				">
				&nbsp;&nbsp;&nbsp;
		        <a href="javascript:void(0)" onclick="query()" id="btn-save" icon="icon-search">查询</a>
		  	</form>
		</div>
<!-- center -->
		<div data-options="region:'center',border:false,split:true" title="report列表">
			<table id="user_info"></table>
		  	<div id="user-window" title="用户窗口" style="width:450px;height:auto;">
				<div style="padding:20px 20px 40px 40px;">
					<form id="userform" method="post" enctype="multipart/form-data">
						<table>
							<tr>
								<td>
									<input type="hidden" class="easyui-validatebox" name="data_id" id="data_id">
									<s:file name="upload" id="upload" label="文件上传" class="easyui-validatebox" required="true"></s:file>
			        			</td>
							</tr>
							<tr>
								<td>备注:</td>
								<td><input type="text" class="easyui-validatebox" name="data_note" id="data_note"></input></td>
							</tr>
							<tr id="isGroupReport">
								<td>小组报告:</td>
								<td>
									是<input type="radio" name="data_report_flag" value="Y"/>&nbsp;&nbsp;&nbsp;&nbsp;
									否<input type="radio" name="data_report_flag" value="N" checked="checked" />
								</td>
							</tr>
						</table>
					</form>
				</div>
				<div style="text-align:center;padding:5px;">
					<a href="javascript:void(0)" onclick="save()" id="btn-save" icon="icon-save">确认</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeWindow()" id="btn-cancel" icon="icon-cancel">取消</a>
				</div>
			</div>
		</div>
		  
		<div data-options="region:'south',border:true,split:true" title="支撑文档列表" style="height:200px;">
			<table id="table_body"></table>
		</div>
	</div>
		
		<script type="text/javascript" src="./jquery-easyui-1.3.2/easyloader.js"></script>
		<script type="text/javascript">
			var user_role_id = <%=session.getAttribute("user_role_id")%>
			if(user_role_id != 1 && user_role_id != 4){
				var toolbar = [{
					id:'addbtn',
					text:'上传',
					iconCls:'icon-add',
					handler:function(){append()}
				},{
					text:'删除',
					iconCls:'icon-cut',
					handler:function(){remove()}
				},{
					text:'下载',
					iconCls:'icon-save',
					handler:function(){download()}
				},{
					text:'返回',
					iconCls:'icon-undo',
					handler:function(){reject()}
				},{
					text:'帮助',
					iconCls:'icon-help',
					handler:function(){reject()}
				}];
				//表体按钮
				var toolbarbody = [{
					text:'上传',
					iconCls:'icon-add',
					handler:function(){appendbody()}
				},{
					text:'删除',
					iconCls:'icon-cut',
					handler:function(){removebody()}
				},{
					text:'下载',
					iconCls:'icon-save',
					handler:function(){downloadbody()}
				},{
					text:'返回',
					iconCls:'icon-undo',
					handler:function(){rejectbody()}
				},{
					text:'帮助',
					iconCls:'icon-help',
					handler:function(){rejectbody()}
				}];
			}else {
				var toolbar = [{
					text:'删除',
					iconCls:'icon-cut',
					handler:function(){remove()}
				},{
					text:'下载',
					iconCls:'icon-save',
					handler:function(){download()}
				},{
					text:'返回',
					iconCls:'icon-undo',
					handler:function(){reject()}
				},{
					text:'帮助',
					iconCls:'icon-help',
					handler:function(){reject()}
				}];
				
				var toolbarbody = [{
					text:'删除',
					iconCls:'icon-cut',
					handler:function(){removebody()}
				},{
					text:'下载',
					iconCls:'icon-save',
					handler:function(){downloadbody()}
				},{
					text:'返回',
					iconCls:'icon-undo',
					handler:function(){rejectbody()}
				},{
					text:'帮助',
					iconCls:'icon-help',
					handler:function(){rejectbody()}
				}];
			}
		</script>
  </body>
</html>
